//
//  LGBreathRecordController.m
//  HeartDisease_YHJK
//
//  Created by 1234 on 2025/7/9.
//  呼吸训练统计

#import "LGBreathRecordChartController.h"
#import "LGPatientBreathRecordChart.h"
#import "DatePickerTwoView.h"
@interface LGBreathRecordChartController ()<DatePickerViewDelegate,UIScrollViewDelegate>
/**   */
@property (nonatomic , strong)UIScrollView *scrollView;
/**  排痰训练统计 */
@property (nonatomic , strong)LGPatientBreathRecordChart *chartView1;
@property (nonatomic , strong)UITextField *startItem1;
@property (nonatomic , strong)UITextField *endItem1;
@property (nonatomic , copy)NSString *startDate1;
@property (nonatomic , copy)NSString *endDate1;

/**  吸气肌训练 */
@property (nonatomic , strong)LGPatientBreathRecordChart *chartView2;
@property (nonatomic , strong)UITextField *startItem2;
@property (nonatomic , strong)UITextField *endItem2;
@property (nonatomic , copy)NSString *startDate2;
@property (nonatomic , copy)NSString *endDate2;
/**   */
@property (nonatomic , strong)NSDateFormatter *formatter;

@end

@implementation LGBreathRecordChartController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createSubView];
    [self initRequestDate];
    [self requestExpectorationData];
    [self requestInspiratoryData];
}

//排痰训练数据
-(void)requestExpectorationData{
    [RequestUtil withPOST:@"/doctor/patient.Physicalsign/pranayamaReportStatistics" parameters:@{@"patient_id":self.patientid,@"type":@"1",@"start_date":self.startDate1,@"end_date":self.endDate1} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.chartView1.dataDic = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

//吸气肌训练数据
-(void)requestInspiratoryData{
    [RequestUtil withPOST:@"/doctor/patient.Physicalsign/pranayamaReportStatistics" parameters:@{@"patient_id":self.patientid,@"type":@"2",@"start_date":self.startDate2,@"end_date":self.endDate2} withSign:YES success:^(NSInteger code, NSString *message, id returnData) {
        if (code == 200) {
            self.chartView2.dataDic = returnData;
        }
    } error:^(NSError *error) {
        
    }];
}

//选择日期
-(void)selectDateAction:(UIButton *)sender{
    NSInteger index = sender.tag - 1000;
    DatePickerTwoView *pickerView = [[DatePickerTwoView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H)];
    pickerView.title = (index == 0||index == 2)?@"开始时间":@"结束时间";
    if (index == 0) {
        pickerView.currentDate = self.startDate1;
    }else if (index == 1){
        pickerView.currentDate = self.endDate1;
    }else if (index == 2){
        pickerView.currentDate = self.startDate2;
    }else{
        pickerView.currentDate = self.endDate2;
    }
    pickerView.index = index;
    pickerView.delegate = self;
    [LGKeyWindow addSubview:pickerView];
}

-(void)sendSelectDate:(NSString *)date index:(NSInteger)index{
    if (index == 0) {
        self.startDate1 = date;
        if ([self needRestDate:self.startDate1 endDate:self.endDate1]) {
            self.endDate1 = @"";
            [TooltipView showMessage:@"请选择结束日期" offset:0];
        }else{
            [self requestExpectorationData];
        }
    }else if (index == 1){
        self.endDate1 = date;
        if ([self needRestDate:self.startDate1 endDate:self.endDate1]) {
            self.startDate1 = @"";
            [TooltipView showMessage:@"请选择开始日期" offset:0];
        }else{
            [self requestExpectorationData];
        }
    }else if (index == 2){
        self.startDate2 = date;
        if ([self needRestDate:self.startDate2 endDate:self.endDate2]) {
            self.endDate2 = @"";
            [TooltipView showMessage:@"请选择结束日期" offset:0];
        }else{
            [self requestInspiratoryData];
        }
    }else{
        self.endDate2 = date;
        if ([self needRestDate:self.startDate2 endDate:self.endDate2]) {
            self.startDate2 = @"";
            [TooltipView showMessage:@"请选择开始日期" offset:0];
        }else{
            [self requestInspiratoryData];
        }
    }
}

-(void)setStartDate1:(NSString *)startDate1{
    _startDate1 = startDate1;
    self.startItem1.text = startDate1;
}

-(void)setEndDate1:(NSString *)endDate1{
    _endDate1 = endDate1;
    self.endItem1.text = endDate1;
}

-(void)setStartDate2:(NSString *)startDate2{
    _startDate2 = startDate2;
    self.startItem2.text = startDate2;
}

-(void)setEndDate2:(NSString *)endDate2{
    _endDate2 = endDate2;
    self.endItem2.text = endDate2;
}

-(void)initRequestDate{
    NSDate *currentDate = [NSDate date];
    // 获取当前日历（Gregorian 日历）
    NSCalendar *calendar = [NSCalendar currentCalendar];
   
   // 创建日期组件，并设置月份的第一天
    NSDateComponents *components = [calendar components:(NSCalendarUnitYear | NSCalendarUnitMonth) fromDate:currentDate];
    components.day = 1; // 设置为 1 号
   
   // 获取本月第一天的日期
    NSDate *firstDay = [calendar dateFromComponents:components];
    self.startDate1 = [self.formatter stringFromDate:firstDay];
    self.startDate2 = [self.formatter stringFromDate:firstDay];
    self.endDate1 = [self.formatter stringFromDate:currentDate];
    self.endDate2 = [self.formatter stringFromDate:currentDate];
}

-(BOOL)needRestDate:(NSString *)start endDate:(NSString *)end{
    NSDate *startDate = [self.formatter dateFromString:start];
    NSDate *endDate = [self.formatter dateFromString:end];
    NSComparisonResult result = [endDate compare:startDate];
    if (result == NSOrderedDescending || result == NSOrderedSame) {
        //end 是未来日期、同一天
        return NO;
    }else{
        return YES;
    }
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    self.chartView1.alertView.hidden = YES;
    self.chartView2.alertView.hidden = YES;
}

#pragma mark
#pragma mark ====> 创建控件
-(void)createSubView{
    self.title = @"呼吸训练统计";
    [self.view addSubview:self.scrollView];
    [self.scrollView addSubview:self.startItem1];
    [self.scrollView addSubview:self.endItem1];
    [self.scrollView addSubview:self.chartView1];
    [self.scrollView addSubview:self.startItem2];
    [self.scrollView addSubview:self.endItem2];
    [self.scrollView addSubview:self.chartView2];
    self.scrollView.contentSize = CGSizeMake(Screen_W, kMaxY(self.chartView2.frame)+viewPix(50));
}

-(UIScrollView *)scrollView{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, Screen_W, Screen_H-topBarHeight)];
        _scrollView.delegate = self;
    }
    return _scrollView;
}

-(UITextField *)startItem1{
    if(!_startItem1){
        _startItem1 = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), viewPix(5), (Screen_W-viewPix(44))/2.0, viewPix(34))];
        _startItem1.tintColor = [UIColor colorWithString:@"#333333"];
        _startItem1.textColor = [UIColor colorWithString:@"#333333"];
        _startItem1.backgroundColor = [UIColor whiteColor];
        _startItem1.textAlignment = NSTextAlignmentLeft;
        _startItem1.keyboardType = UIKeyboardTypeDefault;
        _startItem1.placeholder = @"请选择开始日期";
        _startItem1.cornerRidus = 10;
        _startItem1.font = LGFont(14);
        _startItem1.leftViewMode = UITextFieldViewModeAlways;
        _startItem1.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(34))];
        _startItem1.rightViewMode = UITextFieldViewModeAlways;
        _startItem1.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(30), viewPix(34))];
        UIButton *actionBtn = [UIButton buttonWithImage:@"grayTipDown16" frame:CGRectMake(0, 0, (Screen_W-viewPix(44))/2.0, viewPix(34))];
        actionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        actionBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, viewPix(12));
        actionBtn.tag = 1000;
        [actionBtn addTarget:self action:@selector(selectDateAction:) forControlEvents:UIControlEventTouchUpInside];
        [_startItem1 addSubview:actionBtn];
    }
    return _startItem1;
}

-(UITextField *)endItem1{
    if(!_endItem1){
        _endItem1 = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.startItem1.frame)+viewPix(12), viewPix(5), (Screen_W-viewPix(44))/2.0, viewPix(34))];
        _endItem1.tintColor = [UIColor colorWithString:@"#333333"];
        _endItem1.textColor = [UIColor colorWithString:@"#333333"];
        _endItem1.backgroundColor = [UIColor whiteColor];
        _endItem1.textAlignment = NSTextAlignmentLeft;
        _endItem1.keyboardType = UIKeyboardTypeDefault;
        _endItem1.placeholder = @"请选择结束日期";
        _endItem1.cornerRidus = 10;
        _endItem1.font = LGFont(14);
        _endItem1.leftViewMode = UITextFieldViewModeAlways;
        _endItem1.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(34))];
        _endItem1.rightViewMode = UITextFieldViewModeAlways;
        _endItem1.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(30), viewPix(34))];
        UIButton *actionBtn = [UIButton buttonWithImage:@"grayTipDown16" frame:CGRectMake(0, 0, (Screen_W-viewPix(44))/2.0, viewPix(34))];
        actionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        actionBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, viewPix(12));
        actionBtn.tag = 1001;
        [actionBtn addTarget:self action:@selector(selectDateAction:) forControlEvents:UIControlEventTouchUpInside];
        [_endItem1 addSubview:actionBtn];
    }
    return _endItem1;
}



-(LGPatientBreathRecordChart *)chartView1{
    if(!_chartView1){
        _chartView1 = [[LGPatientBreathRecordChart alloc]initWithType:@"phlegm" frame:CGRectMake(viewPix(16), viewPix(52), Screen_W-viewPix(32), viewPix(320))];
        _chartView1.titleLabel.text = @"排痰训练统计";
    }
    return _chartView1;
}


-(UITextField *)startItem2{
    if(!_startItem2){
        _startItem2 = [[UITextField alloc]initWithFrame:CGRectMake(viewPix(16), kMaxY(self.chartView1.frame)+viewPix(30), (Screen_W-viewPix(44))/2.0, viewPix(34))];
        _startItem2.tintColor = [UIColor colorWithString:@"#333333"];
        _startItem2.textColor = [UIColor colorWithString:@"#333333"];
        _startItem2.backgroundColor = [UIColor whiteColor];
        _startItem2.textAlignment = NSTextAlignmentLeft;
        _startItem2.keyboardType = UIKeyboardTypeDefault;
        _startItem2.placeholder = @"请选择开始日期";
        _startItem2.cornerRidus = 10;
        _startItem2.font = LGFont(14);
        _startItem2.leftViewMode = UITextFieldViewModeAlways;
        _startItem2.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(34))];
        _startItem2.rightViewMode = UITextFieldViewModeAlways;
        _startItem2.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(30), viewPix(34))];
        UIButton *actionBtn = [UIButton buttonWithImage:@"grayTipDown2" frame:CGRectMake(0, 0, (Screen_W-viewPix(44))/2.0, viewPix(34))];
        actionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        actionBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, viewPix(12));
        actionBtn.tag = 1002;
        [actionBtn addTarget:self action:@selector(selectDateAction:) forControlEvents:UIControlEventTouchUpInside];
        [_startItem2 addSubview:actionBtn];
    }
    return _startItem2;
}

-(UITextField *)endItem2{
    if(!_endItem2){
        _endItem2 = [[UITextField alloc]initWithFrame:CGRectMake(kMaxX(self.startItem2.frame)+viewPix(12), kMinY(self.startItem2.frame), (Screen_W-viewPix(44))/2.0, viewPix(34))];
        _endItem2.tintColor = [UIColor colorWithString:@"#333333"];
        _endItem2.textColor = [UIColor colorWithString:@"#333333"];
        _endItem2.backgroundColor = [UIColor whiteColor];
        _endItem2.textAlignment = NSTextAlignmentLeft;
        _endItem2.keyboardType = UIKeyboardTypeDefault;
        _endItem2.placeholder = @"请选择结束日期";
        _endItem2.cornerRidus = 10;
        _endItem2.font = LGFont(14);
        _endItem2.leftViewMode = UITextFieldViewModeAlways;
        _endItem2.leftView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(12), viewPix(34))];
        _endItem2.rightViewMode = UITextFieldViewModeAlways;
        _endItem2.rightView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, viewPix(30), viewPix(34))];
        UIButton *actionBtn = [UIButton buttonWithImage:@"grayTipDown2" frame:CGRectMake(0, 0, (Screen_W-viewPix(44))/2.0, viewPix(34))];
        actionBtn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
        actionBtn.imageEdgeInsets = UIEdgeInsetsMake(0, 0, 0, viewPix(12));
        actionBtn.tag = 1003;
        [actionBtn addTarget:self action:@selector(selectDateAction:) forControlEvents:UIControlEventTouchUpInside];
        [_endItem2 addSubview:actionBtn];
    }
    return _endItem2;
}



-(LGPatientBreathRecordChart *)chartView2{
    if(!_chartView2){
        _chartView2 = [[LGPatientBreathRecordChart alloc]initWithType:@"breath" frame:CGRectMake(viewPix(16), kMaxY(self.startItem2.frame)+viewPix(13), Screen_W-viewPix(32), viewPix(320))];
        _chartView2.titleLabel.text = @"吸气肌训练";
    }
    return _chartView2;
}

-(NSDateFormatter *)formatter{
    if(!_formatter){
        _formatter = [[NSDateFormatter alloc]init];
        _formatter.dateFormat = @"yyyy-MM-dd";
    }
    return _formatter;
}

@end
